<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="请输入房屋名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="houseinfos" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="htitlex" label="房屋名称" width="200"></el-table-column>
      <el-table-column prop="hprice" label="房屋金额" width="100" ></el-table-column>
      <el-table-column prop="harea" label="房屋面积" width="100"></el-table-column>
      <el-table-column prop="hyears" label="房屋年代" width="120"></el-table-column>
      <el-table-column prop="htype" label="房屋类型" width="100">
        <!-- scpoe : 它能获取到当前行的数据-->
        <template scope="scope">
          <span style="color: #11b95c" v-if="scope.row.htype === 15">电梯</span>
          <span style="color: #15e3f6" v-else-if="scope.row.htype === 14">步梯</span>
          <span style="color: #ffd000" v-else-if="scope.row.htype === 16">别墅</span>
          <span style="color: #ee83f4" v-else-if="scope.row.htype === 17">公寓</span>
          <span style="color: #5e7382" v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column prop="hquartersName" label="小区名字" width="160"></el-table-column>
      <el-table-column prop="hstatus" label="房屋状态" width="80">
        <!-- scpoe : 它能获取到当前行的数据-->
        <template scope="scope">
          <span style="color: #d74717" v-if="scope.row.hstatus === 18">已租</span>
          <span style="color: #23ca2e" v-else-if="scope.row.hstatus === 19">未租</span>
          <span style="color: #5e7382" v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column prop="hcreateTime" label="房屋信息发布时间"
                       :formatter="formatDate" width="160"></el-table-column>
      <el-table-column label="操作" width="300">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="primary" size="small" @click="handleRent(scope.$index, scope.row)">租房</el-button>
          <el-button type="success" size="small" @click="handleOffRent(scope.$index, scope.row)">退租</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :current-page="currentPage"
                     :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增或修改模态框-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="房屋名称" prop="htitlex">
          <el-input v-model="editForm.htitlex" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="房屋金额" prop="hprice">
          <el-input v-model="editForm.hprice" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="房屋面积" prop="harea">
          <el-input v-model="editForm.harea" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="房屋年代" prop="hyears">
          <el-date-picker clearable
                          v-model="editForm.hyears"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择房屋年代">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="房屋类型" prop="htype">
          <el-select v-model="editForm.htype" placeholder="请选择">
            <el-option
                v-for="item in houseTypes"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="小区名字" prop="hquartersName">
          <el-input v-model="editForm.hquartersName" auto-complete="off"></el-input>
        </el-form-item>
<!--        <el-form-item label="房屋状态" prop="hstatus">-->
<!--          <el-select v-model="editForm.hstatus" placeholder="请选择">-->
<!--            <el-option-->
<!--                v-for="item in houseStates"-->
<!--                :label="item.name"-->
<!--                :value="item.id">-->
<!--              <span style="float: left">{{ item.name }}</span>-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="发布时间" prop="hcreateTime">-->
<!--          <el-date-picker clearable-->
<!--                          v-model="editForm.hcreateTime"-->
<!--                          type="date"-->
<!--                          value-format="yyyy-MM-dd"-->
<!--                          placeholder="请选择发布时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--租房页面   模态框-->
    <el-dialog :title="title" :visible.sync="rentFormVisible" :close-on-click-modal="false">
        <el-form :model="rentForm" label-width="80px" :rules="rentFormRules" ref="rentForm">
          <el-form-item label="房屋名称" prop="htitlex">
            <el-input v-model="rentForm.htitlex" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="房屋金额" prop="hprice">
            <el-input v-model="rentForm.hprice" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="房屋面积" prop="harea">
            <el-input v-model="rentForm.harea" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="房屋年代" prop="hyears">
            <el-date-picker clearable
                            v-model="rentForm.hyears"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择房屋年代">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="房屋类型" prop="htype">
            <el-select v-model="rentForm.htype" placeholder="请选择">
              <el-option
                  v-for="item in houseTypes"
                  :label="item.name"
                  :value="item.id">
                <span style="float: left">{{ item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="小区名字" prop="hquartersName">
            <el-input v-model="rentForm.hquartersName" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="房屋状态" prop="hstatus">
            <el-radio-group v-model="rentForm.hstatus">
              <el-radio class="radio" :label="18">已租</el-radio>
              <el-radio class="radio" :label="19">未租</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="发布时间" prop="hcreateTime">
            <el-date-picker clearable
                            v-model="rentForm.hcreateTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择发布时间">
            </el-date-picker>
          </el-form-item>
        </el-form>

      <div> <h4>=========================  合同签订  ================================</h4> </div>

      <el-form :model="rentForm2" label-width="80px" :rules="rentFormRules" ref="rentForm2">
        <el-table-column prop="ctId" label="合同编号" width="60">
        </el-table-column>
        <el-form-item label="甲方姓名" prop="ctPartyA" style="width: 400px">
          <el-select v-model="rentForm2.ctPartyA" placeholder="请选择">
            <el-option
                v-for="item in emps"
                :label="item.username"
                :value="item.username">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="乙方姓名" prop="ctPartyB" style="width: 400px">

          <el-select v-model="rentForm2.ctPartyB" placeholder="请选择">
            <el-option
                v-for="item in users"
                :label="item.username"
                :value="item.username">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="甲方身份证信息" prop="idCardA" label-width="120px">
          <el-input v-model="rentForm2.idCardA" auto-complete="off" ></el-input>
        </el-form-item>
        <el-form-item label="乙方身份证信息" prop="idCardB" label-width="120px">
          <el-input v-model="rentForm2.idCardB" auto-complete="off"></el-input>
        </el-form-item>
<!--        <el-form-item label="租赁地址" prop="ctAddress">-->
<!--          <el-input v-model="rentForm2.ctAddress" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="租金" prop="ctRent" style="width: 200px">-->
<!--          <el-input v-model="rentForm2.ctRent" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="付款方式" prop="ctPaymentType">
          <el-select v-model="rentForm2.ctPaymentType" placeholder="请选择">
            <el-option
                v-for="item in options2"
                :label="item.label"
                :value="item.value2">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租赁时间" prop="ctLeaseTime">
          <el-date-picker clearable
                          v-model="rentForm2.ctLeaseTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择租赁时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到期时间" prop="ctExpirationTime">
          <el-date-picker clearable
                          v-model="rentForm2.ctExpirationTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择租赁时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="rentFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="rentSubmit" :loading="rentLoading">生成合同</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>
export default {
  data() {
    return {
      options2: [{
        value2: '2',
        label: '支付宝'
      }, {
        value2: '4',
        label: '微信'
      }, {
        value2: '6',
        label: '网银'
      }],
      houseTypes: [], // 房屋类型
      houseStates: [], // 房屋状态
      emps: [], // 租客 --甲方
      users: [], // 房东 --乙方
      value: '',
      value2: '',
      createTime: '',
      table: false,
      gridData: [],
      title: '',
      filters: {
        keyword: ''
      },
      houseinfos: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      page: 1,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      rentFormVisible: false,//编辑界面是否显示
      editLoading: false,
      rentLoading: false,
      editFormRules: {
        name: [
          {required: true, message: '请输入房屋名称', trigger: 'blur'}
        ],
        sn: [
          {required: true, message: '请输入房屋编号', trigger: 'blur'}
        ]
      },
      rentFormRules: {
        // idCardA: [
        //   {required: true, message: '请输入身份证号', trigger: 'blur'},
        //   {pattern: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/, message: '请输入合法身份证号', trigger: 'blur' }
        // ],
        // idCardB: [
        //   {required: true, message: '请输入身份证号', trigger: 'blur'},
        //   {pattern: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/, message: '请输入合法身份证号', trigger: 'blur' }
        // ],
        // ctPartyA: [
        //   {required: true, message: '请输入中文名', trigger: 'blur'},
        //   {pattern: /^(?:[\u4E00-\u9FA5·]{2,16})$/, message: '请输入中文名', trigger: 'blur' }
        // ],
        // ctPartyB: [
        //   {required: true, message: '请输入中文名', trigger: 'blur'},
        //   {pattern: /^(?:[\u4E00-\u9FA5·]{2,16})$/, message: '请输入中文名', trigger: 'blur' }
        // ]
      },
      //编辑界面数据
      editForm: {
        hId: 0,
        htitlex:'',
        hprice:0,
        harea:0,
        hyears:'',
        htype:'',
        hquartersName:'',
        hstatus:2,
        hcreateTime:'',
        value:''
      },
      //租房页面回显数据
      rentForm: {
        hId: 0,
        htitlex:'',
        hprice:0,
        harea:0,
        hyears:'',
        htype:1,
        hquartersName:'',
        hstatus:1,
        hcreateTime:'',
        value:''
      },

      //租房页面新增合同数据
      rentForm2: {
        ctId:0,
        ctPartyA:'',
        ctPartyB:'',
        idCardA:'',
        idCardB:'',
        ctAddress:'',
        ctLeaseTime:'',
        ctRent:'',
        ctPaymentType:'',
        houseId:0,
        ctExpirationTime:'',
        ctStatus:4,
        value2:'',
        hstatus:1
      },
    }
  },
  methods: {
    //获取租客==甲方列表
    getEmployees() {
      this.$http.get("/emp")
          .then(result => {
            this.emps = result.data;
          })
    },
    //获取房东==乙方列表
    getUsers() {
      this.$http.get("/user")
          .then(result => {
            this.users = result.data;
          })
    },
    //获取房屋类型列表
    getHouseTypes() {
      this.$http.get("/type/findHouseType")
          .then(result => {
            this.houseTypes = result.data;
          })
    },
    //获取房屋状态列表
    getHouseStates() {
      this.$http.get("/detail/findHouseState")
          .then(result => {
            this.houseStates = result.data;
          })
    },
    search() {
      this.currentPage = 1;
      this.getHouseinfos();
    },
    formatDate(row, column) {
      // 获取单元格数据
      let data = row[column.property];
      if(data == null) {
        return null;
      }
      let dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
    },

    handleCurrentChange(val) {
      this.currentPage = val;
      this.getHouseinfos();
    },
    //获取房屋列表
    getHouseinfos() {
      let para = {currentPage: this.currentPage, pageSize: this.pageSize, keyword: this.filters.keyword};
      this.listLoading = true;
      //NProgress.start();
      this.$http.post("/housingInformation/list", para)
          .then(result => {
            // 关闭忙等
            this.listLoading = false;
            this.total = result.data.totals;
            this.houseinfos = result.data.rows;
          })
          .catch(res => {
            this.$message({
              message: "网络不通！",
              type: "error",
            })
          })
    },
    //删除
    handleDel: function (index, row) {
      if (row.hstatus === 18){
        alert("房屋已出租，不能删除信息。")
      } else {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          this.$http.delete("/housingInformation/" + row.hid)
              .then(result => {
                this.listLoading = false;
                result = result.data;
                if (result.success) { // 如果后端返回success
                  this.$message({
                    message: result.msg,
                    type: 'success'
                  });
                  this.currentPage = 1,
                      // 刷新列表
                      this.getHouseinfos();
                } else {
                  this.$message({
                    message: result.msg,
                    type: 'error'
                  });
                }
              }).catch(result => {
            // 1.关闭忙等框
            this.listLoading = false;
            // 2.给出自定义失败提示！
            this.$message({
              message: '系统繁忙，请重试！',
              type: 'error'
            });
          })
        }).catch(() => {
        });
      }
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      if (row.hstatus === 18){
        alert("房屋已出租，不能修改信息。")
      } else {
        this.title = "编辑";
        this.editFormVisible = true;
        this.editForm = Object.assign({}, row);
      }
    },
    //显示新增界面
    handleAdd: function () {
      this.title = "新增";
      this.editFormVisible = true;
      this.editForm = {
        hId: null,
        htitlex:'',
        hprice:'',
        harea:'',
        hyears:'',
        htype:'',
        hquartersName:'',
        hstatus:"",
        hcreateTime:'',
        value:''
      };
    },
    // 显示租房界面
    handleRent: function (index, row) {
      if (row.hstatus === 18){
        alert("房屋已出租，不能重复出租。")
      } else {
        this.title = "租房";
        this.rentFormVisible = true;
        this.rentForm = Object.assign({}, row);
        this.rentForm2 = {
          ctId:null,
          ctPartyA:'',
          ctPartyB:'',
          idCardA:'',
          idCardB:'',
          ctAddress:'',
          ctLeaseTime:'',
          ctRent:'',
          ctPaymentType:'',
          houseId:0,
          ctExpirationTime:'',
          ctStatus:''
        };
      }

    },
    // 租房
    rentSubmit: function () {
      this.$refs.rentForm2.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.rentLoading = true; // 开启提交按钮忙等
            let para = Object.assign({},this.rentForm, this.rentForm2); // 表单数据拷贝
            // 1.发送Axios请求
            this.$http.put("/contractInformation", para)
                .then(result => {
                  // 1.关闭提交按钮忙等
                  this.rentLoading = false;
                  // 2.关闭弹出框
                  this.rentFormVisible = false;
                  // 3.获取后端传递的AjaxResult对象
                  result = result.data;
                  // 4.判断租房是否成功
                  if (result.success) {
                    // 租房成功
                    // 1.给出成功提示
                    this.$message({
                      message: result.msg,
                      type: 'success'
                    });
                    // 2.修改页码为第一页
                    // this.currentPage = 1;
                    // 3.调用函数刷新列表
                    this.getHouseinfos();
                  } else {
                    // 租房失败
                    // 租房成功
                    // 1.给出失败提示
                    this.$message({
                      message: result.msg,
                      type: 'error'
                    });
                    // 2.修改页码为第一页
                    // this.currentPage = 1;
                    // 3.调用函数刷新列表
                    this.getHouseinfos();
                  }
                })
                .catch(result => { // 接口调用失败
                  // 1.关闭忙等框
                  this.listLoading = false;
                  // 2.给出自定义失败提示！
                  this.$message({
                    message: '系统繁忙，请重试！',
                    type: 'error'
                  });
                })
          });
        }
      });
    },
    /* 退租 */
    handleOffRent: function (index, row) {
      this.$confirm('确认退租吗?', '提示', {
        type: 'warning'
      }).then(() => {
        // 列表忙等
        this.listLoading = true;
        // 请求后端接口
        let para = Object.assign({}, row);
        this.$http.put("/housingInformation/offRent",para )
            .then(result => {
              result = result.data;
              if (result.success) {
                this.$message({
                  message: '退租成功',
                  type: 'success'
                });
              } else {
                this.$message({
                  message: result.msg,
                  type: 'error'
                });
              }
              // 关闭忙等
              this.listLoading = false;
              // 刷新列表
              this.currentPage = 1;
              this.getHouseinfos();
            })
            .catch(result => {
              this.listLoading = false;
              this.$message({
                message: '网络不通',
                type: 'error'
              });
            })
      })

    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true; // 开启提交按钮忙等
            let para = Object.assign({}, this.editForm); // 表单数据拷贝
            // 1.发送Axios请求
            this.$http.put("/housingInformation", para)
                .then(result => {
                  // 1.关闭提交按钮忙等
                  this.editLoading = false;
                  // 2.关闭弹出框
                  this.editFormVisible = false;
                  // 3.获取后端传递的AjaxResult对象
                  result = result.data;
                  // 4.判断删除是否成功
                  if (result.success) {
                    // 删除成功
                    // 1.给出成功提示
                    this.$message({
                      message: result.msg,
                      type: 'success'
                    });
                    // 2.修改页码为第一页
                    // this.currentPage = 1;
                    // 3.调用函数刷新列表
                    this.getHouseinfos();
                  } else {
                    // 删除失败
                    // 删除成功
                    // 1.给出失败提示
                    this.$message({
                      message: result.msg,
                      type: 'error'
                    });
                    // 2.修改页码为第一页
                    // this.currentPage = 1;
                    // 3.调用函数刷新列表
                    this.getHouseinfos();
                  }
                })
                .catch(result => { // 接口调用失败
                  // 1.关闭忙等框
                  this.listLoading = false;
                  // 2.给出自定义失败提示！
                  this.$message({
                    message: '系统繁忙，请重试！',
                    type: 'error'
                  });
                })
          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.hid);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.patch("/housingInformation", ids)
            .then(result => {
              result = result.data;
              if (result.success) { // 如果后端返回success
                this.$message({
                  message: result.msg,
                  type: 'success'
                });
                this.currentPage = 1,
                    // 刷新列表
                    this.getHouseinfos();
              } else {
                this.$message({
                  message: result.msg,
                  type: 'error'
                });
              }

            })
            .catch(result => { // 接口调用失败
              // 1.关闭忙等框
              this.listLoading = false;
              // 2.给出自定义失败提示！
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getHouseinfos();
    this.getHouseTypes();
    this.getHouseStates();
    this.getUsers();
    this.getEmployees();
  }
}

</script>

<style scoped>

</style>